/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.tween;

import org.chaoticengine.cgll.entity.Entity;
import org.newdawn.slick.geom.Vector2f;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class PositionTween extends Tween {
    protected Vector2f sPos = new Vector2f();
    protected Vector2f ePos = new Vector2f();

    public PositionTween(Vector2f startPos, Vector2f endPos, int duration) {
        super(duration);
        this.sPos = startPos;
        this.ePos = endPos;
    }

    @Override
    public void doUpdate(int delta) {
        if (repeat.equals(RepeatMode.None)) {
            // Do nothing
        } else if (repeat.equals(RepeatMode.Loop)) {
            // Restart
            if (timePassed >= duration) {
                timePassed = 0;
            }
        } else if (repeat.equals(RepeatMode.PingPong)) {
            // Swap start & end values and restart
            if (timePassed >= duration) {
                timePassed = 0;
                
                Vector2f p = ePos;
                ePos = sPos;
                sPos = p;
            }

            Entity e = getOwner().getOwner();
            if (e != null) {
                Vector2f change = ePos.copy().sub(sPos).scale(timePassed / (float) duration);
                e.setPosition(sPos.copy().add(change));
            }
        }

        Entity e = getOwner().getOwner();
        if (e != null) {
            Vector2f change = ePos.copy().sub(sPos).scale(timePassed / (float) duration);
            e.setPosition(sPos.copy().add(change));
        }
    }
}
